//
//  NKBarChartWeeklyView.swift
//  Nokia
//
//  Created by 易先生 on 2022/4/1.
//

import UIKit
import MYModule

class DataBarChartWeeklyView: View {

    private var titles: [String] = []
    
    public var textFont: UIFont = Fonts.systemFont(ofSize: 12)
    
    public var textColor: UIColor = UIColor.assist_A9A9A9

    override func makeUI() {
        super.makeUI()
        
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    /// 数据匹配
    /// - Parameters:
    ///   - model: 数据源
    public func configData(datas : [String]) {
        titles = datas
        collectionView.reloadData()
    }
    
    private lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayoutView)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isScrollEnabled = false
        collectionView.register(DataWeeklyBarTitleCollectionCell.self, forCellWithReuseIdentifier: DataWeeklyBarTitleCollectionCell.reuseIdentifier)
        return collectionView
    }()
    
    public lazy var flowLayoutView: UICollectionViewFlowLayout = {
        let flowLayoutView = UICollectionViewFlowLayout()
        flowLayoutView.scrollDirection = .horizontal
        return flowLayoutView
    }()

}

extension DataBarChartWeeklyView : UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titles.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let titleStr = titles[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DataWeeklyBarTitleCollectionCell.reuseIdentifier, for: indexPath) as! DataWeeklyBarTitleCollectionCell
        cell.configStr(title: titleStr, textFont: textFont, textColor: textColor)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}

extension DataBarChartWeeklyView : UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.frame.width/CGFloat(titles.count), height: self.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

